package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.entity.Html;
import com.baichang.taoshunda.service.HtmlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2017/9/20 0020.
 */
@Controller
@RequestMapping(ControllerConstants.HTML)
public class HtmlWController extends WebBaseController {

    @Autowired
    private HtmlService htmlService;

    /**
     * 查询用户协议
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/findProtocolIndex")
    public String findProtocol(HttpServletRequest request, HttpServletResponse response, Model model) {
//        Html html = htmlService.queryHtmlInfo();
//        model.addAttribute("id", html.getId());
//        model.addAttribute("name", html.getName());
//        model.addAttribute("value", html.getValue());
        return "/taoshunda/protocol/protocol";
    }

    /**
     * 修改
     *
     * @param request
     * @param response
     * @param model
     */
    @RequestMapping("/update")
    public void update(final HttpServletRequest request, HttpServletResponse response, Model model) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "id", 0);
                String name = getString(request, "name", "");
                String value = getString(request, "value", "");
                return htmlService.update(id, name, value);
            }
        });
    }

    /**
     * 查询协议
     * @param request
     * @param response
     */
    @RequestMapping("/queryHtml")
    public void queryHtml(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                return htmlService.queryHtmlInfo(nowPage, pageSize);
            }
        });
    }
}
